package com.jsu.utils;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Base64;


public class Base64Utils {

    // 压缩图片
    public static BufferedImage compressImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
        int type = originalImage.getType() == 0 ? Transparency.TRANSLUCENT : originalImage.getType();
        BufferedImage compressedImage = new BufferedImage(targetWidth, targetHeight, type);
        Graphics2D graphics = compressedImage.createGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
        graphics.dispose();
        return compressedImage;
    }

    // 将图片转换为 Base64 编码字符串
    public static String imageToBase64(BufferedImage image) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", outputStream);
        byte[] imageBytes = outputStream.toByteArray();
        return Base64.getEncoder().encodeToString(imageBytes);
    }

    public static void main(String[] args) {
        try {
            // 加载原始图片
            BufferedImage originalImage = ImageIO.read(new URL("https://wscc5-1324713586.cos.ap-guangzhou.myqcloud.com/%E7%B2%BE%E5%BD%A9%E5%9B%BE%E7%89%87/%E5%A4%A7%E4%BC%9A%E9%A2%84%E7%83%AD/05909e2f5d4943368d9eb38f4e69bb5b.jpg"));

            // 指定压缩后的图片尺寸
            int targetWidth = 300;
            int targetHeight = 200;

            // 压缩图片
            BufferedImage compressedImage = compressImage(originalImage, targetWidth, targetHeight);

            // 将压缩后的图片转换为 Base64 编码字符串
            String base64Image = imageToBase64(compressedImage);

            System.out.println("压缩后的图片转换为 Base64 编码字符串：");
            System.out.println(base64Image);
            base64ToMat(base64Image);
            System.out.println(base64ToMat(base64Image));
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static Mat matURL(URL url) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        BufferedImage originalImage = null;
        BufferedImage compressedImage = null;
        String base64Image = null;
        try {
            //拿到原始图片和压缩
            originalImage = ImageIO.read(url);
            compressedImage = compressImage(originalImage, 300, 200);
            base64Image = imageToBase64(compressedImage);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return base64ToMat(base64Image);
    }

    /**
     * 将base64字符串解码为Mat对象
     * @param base64Data
     * @return
     */
    public static Mat base64ToMat(String base64Data) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        try {
            // 解码 Base64 编码的图像数据为字节数组
            byte[] imageData = Base64.getDecoder().decode(base64Data);
            // 将字节数组转换为 OpenCV 中的 Mat 对象
            Mat mat = Imgcodecs.imdecode(new MatOfByte(imageData), Imgcodecs.IMREAD_UNCHANGED);
            return mat;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}